/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      DocumentBuilder.hpp
 *
 * $FTS$
 */
#ifndef _DOCUMENT_BUILDER_HPP__
#define _DOCUMENT_BUILDER_HPP__ 1

/**
  @file DocumentBuilder.hpp
  @brief Document builder class
*/

#include <Document.hpp>
#include <STLVector.hpp>
#include <STLString.hpp>

namespace FTSS // FullTextSearchSystem
{

/**
  @class DocumentBuilder DocumentBuilder.hpp <DocumentBuilder.hpp>
  @brief Document builder class
*/
class DocumentBuilder
{
public:
	/**
	  @brief Add term to the document
	  @param sTerm - term to add
	  @param iTermLength - length of term
	  @return 0 if success, -1 if any error occured
	*/
	inline INT_32 AddTerm(const UCCHAR_P & sTerm, const UINT_32 & iTermLength)
	{
		// Too big term
		if (iTermLength >= (1 << 16)) { return -1; }

		// Store in new part
		if (iCurrentOffset >= (1 << 16))
		{
			iCurrentOffset = 0;
			vParts.push_back(DocumentPart(sData.size(), vIndex.size()));
		}

		sData.append((CCHAR_P)sTerm, iTermLength);

		vIndex.push_back(DocumentIndex(iCurrentOffset, iTermLength));

		iCurrentOffset += iTermLength;
	return 0;
	}

	/**
	  @brief Add data between terms
	  @param sSpace - data to add
	  @param iDataLength - length of data
	  @return 0 if success, -1 if any error occured
	*/
	inline INT_32 AddWhiteSpace(const UCCHAR_P & sSpace, const UINT_32 & iDataLength)
	{
		// Too big data
		if (iDataLength >= (1 << 16)) { return -1; }

		sData.append((CCHAR_P)sSpace, iDataLength);

		iCurrentOffset += iDataLength;

	return 0;
	}

	/**
	  @brief Add data between terms with special mark
	  @param sSpace - data to add
	  @param iDataLength - length of data
	  @param iDataSplitter - data splitter flag
	  @return 0 if success, -1 if any error occured
	*/
	inline INT_32 AddMarkedWhiteSpace(const UCCHAR_P & sSpace, const UINT_32 & iDataLength, const CHAR_8 & iDataSplitter)
	{
		// Too big data
		if (iDataLength >= (1 << 16)) { return -1; }

		sData += iDataSplitter;
		++iCurrentOffset;
		sData.append((CCHAR_P)sSpace, iDataLength);

		iCurrentOffset += iDataLength;

	return 0;
	}

	/**
	  @brief Start new document
	  @return 0 if success, -1 if any error occured
	*/
	inline INT_32 StartDocument()
	{
		iCurrentOffset = 0;
		iCurrentPart   = 0;

		STLW::string          sTMP; sTMP.swap(sData);
		{ STLW::vector<DocumentPart>  vTMP; vTMP.swap(vParts); }
		{ STLW::vector<DocumentIndex> vTMP; vTMP.swap(vIndex); }
	return 0;
	}

	/**
	  @brief End of the document
	  @return 0 if success, -1 if any error occured
	*/
	inline INT_32 EndDocument()
	{
		vParts.push_back(DocumentPart(sData.size(), vIndex.size()));
		vIndex.push_back(DocumentIndex(iCurrentOffset, 0));

	return 0;
	}

	/**
	  @brief Get parts of the document
	  @param aParts - parts of document
	  @param iPartsSize - size of parts
	  @param aIndex - index with offsets
	  @param iIndexSize - size of index
	  @param sText - text data
	  @param iTextSize - text size
	  @return 0 if success, -1 if any error occured
	*/
	inline INT_32 GetDocumentParts(const DocumentPart * & aParts, UINT_32 & iPartsSize, const DocumentIndex * & aIndex, UINT_32 & iIndexSize, UCCHAR_P & sText, UINT_32 & iTextSize)
	{
		aParts = &vParts[0];
		aIndex = &vIndex[0];

		iPartsSize = vParts.size() * sizeof(DocumentPart);
		iIndexSize = vIndex.size() * sizeof(DocumentIndex);
		sText = (UCCHAR_P)sData.c_str();
		iTextSize = sData.size();

	return 0;
	}

	/**
	  @brief A drestructor
	*/
	inline ~DocumentBuilder() throw() { ;; }
private:
	/** Document parts           */
	std::vector<DocumentPart>   vParts;
	/** Index with token offsets */
	STLW::vector<DocumentIndex> vIndex;
	/** Token list               */
	STLW::string                sData;
	/** Current size of tokens   */
	UINT_32                     iCurrentOffset;
	/** Offset of current part of document */
	UINT_32                     iCurrentPart;
};

} // namespace FTSS
#endif // _DOCUMENT_BUILDER_HPP__
// End.
